import { defineStore } from "pinia";
import { MenuState } from "../interface";
import piniaPersistConfig from "@/stores/helper/persist";
import { MenuOptions } from "@/biz/interface/menu";
import { getAllBreadcrumbList, getFlatMenuList } from "@/utils";

// MenuStore
export const useMenuStore = defineStore({
  id: "MenuState",
  state: (): MenuState => ({
    // menu collapse
    isCollapse: false,
    // menu List
    menuList: []
  }),
  getters: {
    // 菜单权限列表 ==> 扁平化之后的一维数组菜单，主要用来添加动态路由
    flatMenuListGet: state => getFlatMenuList(state.menuList),
    // 递归处理后的所有面包屑导航列表
    breadcrumbListGet: state => getAllBreadcrumbList(state.menuList)
  },
  actions: {
    async setCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    async setMenuList(menuList: MenuOptions[]) {
      this.menuList = menuList;
    }
  },
  persist: piniaPersistConfig("MenuState")
});
